﻿using System;
namespace Microsoft.Extensions.Logging
{
    internal static class LoggingExtensions
    {
        private static readonly Action<ILogger, string, Exception> _redirectUrl;
        private static readonly Action<ILogger, string, Exception> _authorizeUrl;
        private static readonly Action<ILogger, string, Exception> _authorizeCallbackUrl;
        private static readonly Action<ILogger, string, Exception> _requestOpenIdFailed;
        private static readonly Action<ILogger, string, Exception> _loginUrl;
        private static readonly Action<ILogger, string, Exception> _userInfoResponse;
        private static readonly Action<ILogger, string, Exception> _userInfoResponseFailed;


        static LoggingExtensions()
        {
            _redirectUrl = LoggerMessage.Define<string>(
                eventId: new EventId(1, "RedirectUrl"),
                logLevel: LogLevel.Debug,
                formatString: "微信网页授权回调地址:{RedirectUrl}");

            _authorizeUrl = LoggerMessage.Define<string>(
                eventId: new EventId(2, "AuthorizeUrl"),
                logLevel: LogLevel.Debug,
                formatString: "微信网页授权地址:{AuthorizeUrl}");

            _authorizeCallbackUrl = LoggerMessage.Define<string>(
                eventId: new EventId(3, "AuthorizeCallbackUrl"),
                logLevel: LogLevel.Debug,
                formatString: "微信网页授权回调:{Url}");
            
            _requestOpenIdFailed = LoggerMessage.Define<string>(
                eventId: new EventId(4, "RequestOpenIdFailed"),
                logLevel: LogLevel.Error,
                formatString: "微信OpenId获取失败:{Error}");

            _loginUrl = LoggerMessage.Define<string>(
                eventId: new EventId(5, "LoginUrl"),
                logLevel: LogLevel.Debug,
                formatString: "LoginUrl:{Url}");

            _userInfoResponse = LoggerMessage.Define<string>(
                eventId: new EventId(6, "ResponseText"),
                logLevel: LogLevel.Debug,
                formatString: "微信用户信息:{ResponseText}");

            _userInfoResponseFailed = LoggerMessage.Define<string>(
                eventId: new EventId(7, "ResponseText"),
                logLevel: LogLevel.Error,
                formatString: "微信用户信息获取失败:{ResponseText}");
        }

        public static void RedirectUrl(this ILogger logger, string redirectUrl)
        {
            _redirectUrl(logger, redirectUrl, null);
        }

        public static void AuthorizeUrl(this ILogger logger, string authorizeUrl)
        {
            _authorizeUrl(logger, authorizeUrl, null);
        }

        public static void AuthorizeCallbackUrl(this ILogger logger, string url)
        {
            _authorizeCallbackUrl(logger, url, null);
        }
        
        public static void RequestOpenIdFailed(this ILogger logger, string error)
        {
            _requestOpenIdFailed(logger, error, null);
        }

        public static void LoginUrl(this ILogger logger, string url)
        {
            _loginUrl(logger, url, null);
        }

        public static void UserInfoResponse(this ILogger logger, string responseText)
        {
            _userInfoResponse(logger, responseText, null);
        }

        public static void UserInfoResponseFailed(this ILogger logger, string responseText)
        {
            _userInfoResponseFailed(logger, responseText, null);
        }
    }
}
